import { lyric } from 'api/lyric'

// const reg = /\[(.+?)\]/

class Song {
  constructor({
    id,
    name,
    publishTime,
    al,
    ar,
    artists,
    alia,
    mv,
    picUrl,
    rankName,
    dt
  }) {
    this.id = id
    this.name = name,
    this.publishTime = new Date(publishTime).format('yyyy-MM-dd')
    this.mv = mv
    this.alia = alia
    this.dt = dt / 1000 // 歌曲时间

    this.picUrl = picUrl ? picUrl : al.picUrl // 专辑

    const arts = ar || artists // 歌手
    this.singers = arts.map(item => item.name).join(',')

    this.artists = arts
    if (al) {
      this.al = al
      this.albumName = al.name
      this.artistsNames = ar.map(item => item.name).join('/')
    }
    this.rankName = rankName
  }

  async getLyric() {
    if (this.lyric) return this.lyric
    const res = await lyric(this.id)
    const { lrc, nolyric } = res
    this.nolyric = nolyric
    this.lyric = lrc ? lrc.lyric : ''
    return this.lyric
  }
}

export default Song
